﻿using System;

using Centido.Core;

namespace Centido.Management.ConsoleManager
{
    /// <summary>
    ///   An abstract console command that works with table permissions.
    /// </summary>
    internal abstract class PermissionCommandBase : Command
    {
        /// <summary>
        ///   Gets the data action from the specified string
        /// </summary>
        /// <param name="dataActionStr">The specified string.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="dataActionStr"/> is null.</exception>
        /// <returns>Returns the data action specified in the string if there is one; otherwise, returns null.</returns>
        protected DataAction? GetDataAction(string dataActionStr)
        {
            if (dataActionStr == null)
                throw new ArgumentNullException("dataActionStr");
            
            dataActionStr = dataActionStr.ToLower();
             
            switch (dataActionStr)
            {
                case "select":
                    return DataAction.Select;
                case "insert":
                    return DataAction.Insert;
                case "update":
                    return DataAction.Update;
                case "delete":
                    return DataAction.Delete;
                default:
                    return null;
            }
        }


        /// <summary>
        ///   Gets the access from the specified string.
        /// </summary>
        /// <param name="accessLevelStr">The specified string.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="accessLevelStr"/> is null.</exception>
        /// <returns>Returns the access level specified in the string if there is one; otherwise, returns null.</returns>
        protected AccessLevel? GetAccessLevel(string accessLevelStr)
        {
            if (accessLevelStr == null)
                throw new ArgumentNullException("accessLevelStr");

            accessLevelStr = accessLevelStr.ToLower();

            switch (accessLevelStr)
            {
                case "all":
                    return AccessLevel.All;
                case "childandparentunits":
                    return AccessLevel.ChildAndParentUnits;
                case "childunits":
                    return AccessLevel.ChildUnits;
                case "parentunits":
                    return AccessLevel.ParentUnits;
                case "unit":
                    return AccessLevel.Unit;
                case "own":
                    return AccessLevel.Own;
                default:
                    return null;
            }
        }
    }
}
